package com.hcb.micro.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.AccountPlatform;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.ProductSeller;
import com.hcb.micro.dto.ShopMessage;
import com.hcb.micro.service.IAccountPlatformService;
import com.hcb.micro.service.IProductSellerService;
import com.hcb.micro.service.IQqbsAccountService;
import com.hcb.micro.service.IShopMessageService;
import com.hcb.micro.dto.QqbsAccount;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("shopMessage")
public class ShopMessageController extends BaseControllers{
	
	@Autowired
	IAccountPlatformService accountPlatformService;
	
	@Autowired
	IQqbsAccountService qqbsAccountService;
	
	@Autowired
	IShopMessageService shopMessageService;
	
	@Autowired
	IProductSellerService productSellerService;

	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		if (bodyInfo.get("accountId") != null && !"".equals(bodyInfo.get("accountId"))) {
			para.put("accountId", bodyInfo.getString("accountId"));
		}
		
		para.put("isShop", (byte)1);
		
		List<AccountPlatform> apAllList = accountPlatformService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		List<AccountPlatform> apList = accountPlatformService.selectByPara(para);
		
		if(apList.size() <= 0){
			json.put("result", "1");
			json.put("description", "体验店列表不存在");
			return buildReqJsonObject(json);
		}
		
		List<Map<String,Object>> list = new ArrayList<>();
		for(AccountPlatform ap:apList){
			QqbsAccount qae = qqbsAccountService.selectByAccountId(ap.getAccountId());
			ShopMessage sm = shopMessageService.selectByAccountId(ap.getAccountId());
			
			Map<String,Object> map = new HashMap<>();
			map.put("id", ap.getId()+"");
			map.put("accountId", ap.getAccountId()+"");
			map.put("nickname", qae==null?"":qae.getNickname());
			map.put("shopName", sm==null?"":sm.getShopName());
			map.put("shopAddress", sm==null?"":sm.getShopAddress());
			map.put("shopPhone", sm==null?"":sm.getShopPhone());
			map.put("shopWechatNumber", sm==null?"":sm.getShopWechatNumber());
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取体验店列表成功");
		json.put("list", list);
		json.put("count", apAllList.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
        JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("accountId") == null || "".equals(bodyInfo.get("accountId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		AccountPlatform ap = accountPlatformService.selectByAccountId(Integer.valueOf(bodyInfo.getString("accountId")));
		if(ap.getIsShop() != 1){
			json.put("result", "1");
			json.put("description", "该用户不是体验店，无法操作");
			return buildReqJsonObject(json);
		}

		QqbsAccount qae = qqbsAccountService.selectByAccountId(ap.getAccountId());
		ShopMessage sm = shopMessageService.selectByAccountId(ap.getAccountId());
		
		Map<String,Object> map = new HashMap<>();
		map.put("id", ap.getId()+"");
		map.put("accountId", ap.getAccountId()+"");
		map.put("nickname", qae==null?"":qae.getNickname());
		map.put("shopName", sm==null?"":sm.getShopName());
		map.put("shopAddress", sm==null?"":sm.getShopAddress());
		map.put("shopPhone", sm==null?"":sm.getShopPhone());
		map.put("shopWechatNumber", sm==null?"":sm.getShopWechatNumber());
		
		//供应商列表
        Map<String,Object> productSellerPara = new HashMap<>();
		List<ProductSeller> productSellerAllList = productSellerService.selectAllByPara(productSellerPara);
		List<Map<String,Object>> psList = new ArrayList<>();
		if(productSellerAllList.size()>0){
			for (ProductSeller productSeller: productSellerAllList)
	        {	
	        	Map<String,Object> psMap = new HashMap<>();
	        	
	        	psMap.put("hava", 0);
	        	
	        	if(sm != null && sm.getShopCategory() != null){
	        		String[] sellerIdStr = sm.getShopCategory().split(",");
		        	checkSellerId:for(String sellerId:sellerIdStr){
		        		if(sellerId.equals(productSeller.getId()+"")){
		        			psMap.put("hava", 1);
		        			break checkSellerId;
		        		}
		        	}
	        	}
	        	
	        	psMap.put("id", productSeller.getId());
	        	psMap.put("name", productSeller.getName());
	        	psList.add(psMap);
	        }
			json.put("productSellerList", psList);
		}
		
    	json.put("result", "0");
		json.put("description", "获取体验店信息成功");
		json.put("shopMessage", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		net.sf.json.JSONArray saleSellerArea = bodyInfo.getJSONArray("saleSellerArea");
		
		int right = 1;
		if(bodyInfo.get("accountId") == null || "".equals(bodyInfo.get("accountId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		AccountPlatform ap = accountPlatformService.selectByAccountId(Integer.valueOf(bodyInfo.getString("accountId")));
		if(ap.getIsShop() != 1){
			json.put("result", "1");
			json.put("description", "该用户不是体验店，无法操作");
			return buildReqJsonObject(json);
		}
		
		ShopMessage sm = shopMessageService.selectByAccountId(ap.getAccountId());
		
		boolean isNew = false;
		if(sm == null){
			sm = new ShopMessage();
			sm.setAccountId(ap.getAccountId());
			isNew = true;
		}
		
		if(bodyInfo.get("shopName") != null && !"".equals(bodyInfo.get("shopName"))){
			sm.setShopName(bodyInfo.getString("shopName"));
		}
		if(bodyInfo.get("shopAddress") != null && !"".equals(bodyInfo.get("shopAddress"))){
			sm.setShopAddress(bodyInfo.getString("shopAddress"));
		}
		if(bodyInfo.get("shopPhone") != null && !"".equals(bodyInfo.get("shopPhone"))){
			sm.setShopPhone(bodyInfo.getString("shopPhone"));
		}
		if(bodyInfo.get("shopWechatNumber") != null && !"".equals(bodyInfo.get("shopWechatNumber"))){
			sm.setShopWechatNumber(bodyInfo.getString("shopWechatNumber"));
		}
		
		String shopCategory = "";
		if(saleSellerArea.size()>0 && saleSellerArea != null){
			for(int i=0;i<saleSellerArea.size();i++){
				if(saleSellerArea.get(i) != null){
					shopCategory += saleSellerArea.get(i).toString()+",";
				}
			}
		}
		sm.setShopCategory(shopCategory);
		
		Integer rs = 0;
		
		if(isNew){
			rs = shopMessageService.insertSelective(sm);
		}else{
			rs = shopMessageService.updateByPrimaryKeySelective(sm);
		}
		
		if(rs != 1){
			System.out.println("---更新体验店信息失败---");
			json.put("result", "0");
			json.put("description", "更新体验店信息失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "更新体验店信息成功");
		json.put("accountId", bodyInfo.get("accountId"));
		return buildReqJsonObject(json);
	}
}
